import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import at.fhj.ase.clientarch.common.util.EncryptorUtil;
import at.fhj.ase.clientarch.server.db.hibernate.entities.EntityHardwareData;
import at.fhj.ase.clientarch.server.db.hibernate.entities.EntityUser;
import at.fhj.ase.clientarch.server.util.JPAUtil;

public class TestHibernateAndEntities {

  /* 
   * Noch keine ernst zu nehmenden Test-Cases hier drin...
   */

  /** @deprecated never worked, just pure hibernate */
  public void testHibernatePure() {
    SessionFactory sf;
    Transaction tx = null;
    Session session = null;
    sf = new Configuration().configure().buildSessionFactory();

    EntityUser user = new EntityUser("asdf", "asdf");
    session = sf.openSession();
    tx = session.beginTransaction();
    session.save(user);
    tx.commit();
  }

  @Test
  public void testHibernateJPA() {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("cpustatistics_HSQL");

    EntityUser user = new EntityUser("asdf", /*EncryptorUtil.encrypt("asdfPW")*/"asdfPW");

    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();
    em.persist(user);
    transaction.commit();

    EntityHardwareData ehd = new EntityHardwareData(1, 1);
    em.getTransaction().begin();
    em.persist(ehd);
    em.getTransaction().commit();
  }

  @Test
  public void testHibernateJPAUtil() {
    JPAUtil.createDefaultSessionFactory();
    final EntityManager em = JPAUtil.getEmFactory().createEntityManager();
    final EntityTransaction transaction = em.getTransaction();

    final EntityHardwareData ehd = new EntityHardwareData(1, 1);
    transaction.begin();
    em.persist(ehd);
    transaction.commit();
  }

  //  @Test
  //  public void testDatabaseConnector() {
  //    DatabaseController instance = DatabaseController.getInstance(); // just init the hsqldb database
  //    List<EntityHardwareData> allHardwareData = instance.getHardwareDataDao().findAll();
  //    
  //    for (EntityHardwareData hwData: allHardwareData) {
  //      System.out.println(hwData);
  //    }
  //    
  //    instance.shutdownDatabase(); // TODO ends in exception currently but we should close the database...
  //  }
}
